<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="批次号" prop="mesNo">
        <el-input v-model="queryParams.mesNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务编号" prop="taskNo">
        <el-input v-model="queryParams.taskNo" placeholder="请输入任务编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['zndd:task:add']">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['zndd:task:import']">一键导入任务</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" :disabled="sheng" size="mini" @click="oneTouch"
          v-hasPermi="['zndd:workorder:add']">一键生成工单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" :disabled="pc" size="mini" @click="twoTouch"
          v-hasPermi="['zndd:workorder:add']">一键排产</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="workDelete"
          v-hasPermi="['zndd:workorder:remove']" :disabled="shang">删除工单</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['zndd:task:edit']">修改</el-button>
      </el-col> -->
<!--      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['zndd:task:remove']">删除任务</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['zndd:task:import']">导入</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['zndd:task:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="index" label="序号" />
      <el-table-column label="批次号" align="center" prop="mesNo" width="180" />
      <el-table-column label="任务编号" align="center" prop="taskNo" width="180" />
      <el-table-column label="当天任务次序" align="center" prop="taskOrder" width="180" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品编号" align="center" prop="productNo" width="180" />
      <el-table-column label="任务名称" align="center" prop="taskName"  width="180"/>
      <el-table-column label="任务数量" align="center" prop="taskNumber" />
      <el-table-column label="预计开始时间" align="center" prop="exStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计完成时间" align="center" prop="exEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际开始时间" align="center" prop="acStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际结束时间" align="center" prop="acEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计超时时间" align="center" prop="exOverTime"  width="180">
        <template slot-scope="scope">
          <span> {{ formatTime(scope.row.exOverTime) }} </span>
        </template>
      </el-table-column>
      <el-table-column label="实际超时时间" align="center" prop="acOverTime"  width="180">
        <template slot-scope="scope">
          <span> {{ formatTime(scope.row.acOverTime) }} </span>
        </template>
      </el-table-column>
      <el-table-column label="铺线预计完成时间" align="center" prop="lineLayingExEndTime"  width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lineLayingExEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="铺线预计超时时间" align="center" prop="lineLayingExOverTime"  width="180">
        <template slot-scope="scope">
          <span> {{ formatTime(scope.row.lineLayingExOverTime) }} </span>
        </template>
      </el-table-column>

      <el-table-column label="任务节拍(秒)" align="center" width="180" prop="taskRhythm" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"  fixed="right" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['zndd:task:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['zndd:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改任务计划信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="批次号" prop="mesNo">
              <el-input v-model="form.mesNo" placeholder="请输入批次号" :show-overflow-tooltip="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务编号" prop="taskNo">
              <el-input v-model="form.taskNo" placeholder="请输入任务编号" :show-overflow-tooltip="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="taskName">
              <el-input v-model="form.taskName" placeholder="请输入任务名称" :show-overflow-tooltip="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务数量" prop="taskNumber">
              <el-input v-model.number="form.taskNumber" placeholder="请输入任务数量" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="预计开始时间" prop="exStartTime">
              <el-date-picker clearable v-model="form.exStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择预计开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计完成时间" prop="exEndTime">
              <el-date-picker clearable v-model="form.exEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择预计完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="实际开始时间" prop="acStartTime">
              <el-date-picker disabled clearable v-model="form.acStartTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择实际开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际结束时间" prop="acEndTime">
              <el-date-picker disabled clearable v-model="form.acEndTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择实际结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预计超时时间" prop="exOverTime">
              <el-input v-model="form.exOverTime" placeholder="请输入预计超时时间" :show-overflow-tooltip="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际超时时间" prop="acOverTime">
              <el-input v-model="form.acOverTime" placeholder="请输入实际超时时间" :show-overflow-tooltip="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :span="12">
          <el-col :span="12">
            <el-form-item label="产品" prop="productId">
              <el-select v-model="form.productId" placeholder="请选择产品">
                <el-option v-for="item in productList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务节拍" prop="taskRhythm">
              <el-input disabled v-model="form.taskRhythm" placeholder="请输入任务节拍" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="35px" label-position="left" label="/秒" />
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
       </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 任务导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经任务计划数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/zndd/task/task";
import { getToken } from "@/utils/auth";
import { workdelete,addsheng,addpc,paddsheng,paddpc} from "@/api/zndd/work/workorder";
import { listProducttype } from "@/api/zndd/product/producttype";
export default {
  name: "Task",
  data() {
    return {
      //产品数组
      productList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务计划信息表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //任务计划信息导入参数
      upload: {
        // 是否显示弹出层（任务计划信息导入）
        open: false,
        // 弹出层标题（任务计划信息导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的任务计划信息数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/zndd/task/importData"
      },

      sheng:true,
      shang:true,
      pc:true,
       // 工单查询参数
      workParams: {
        pageNum: 1,
        pageSize: 100,
        deptId: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        mesNo: null,
        taskNo: null,
        taskName: null,
        taskNumber: null,
        exStartTime: null,
        exEndTime: null,
        acStartTime: null,
        acEndTime: null,
        exOverTime: null,
        acOverTime: null,
        taskRhythm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mesNo: [
          { required: true, message: "mes批次号不能为空", trigger: "blur" }
        ],
        taskNo: [
          { required: true, message: "任务编号不能为空", trigger: "blur" }
        ],
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],

        taskNumber: [
          { type: 'number', required: true, trigger: ["blur", "change"], message: "任务数量必须为数字值" }
        ],
        exStartTime: [
          { required: true, message: "预计开始时间不能为空", trigger: "blur" }
        ],
        exEndTime: [
          { required: true, message: "预计结束时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    var param = {
      pageNum: 1,
      pageSize: 10,
    };
    //获取产品数组
    listProducttype(param).then(response => {
      var list = [];
      response.rows.forEach(row => {
        list.push({
          label: row.productTypeName,
          value: row.id
        })
      });
      this.productList = list;
    });

  },
  methods: {
    formatTime(totalSeconds) {
      let timeString = '';
      if (totalSeconds >= 3600){
        const hour = Math.floor(totalSeconds / 3600);
        const minutes = Math.floor(totalSeconds / 60) % 60;
        const seconds = Math.floor(totalSeconds % (3600 * hour) % 60);
        timeString = `${hour}小时${minutes}分${seconds}秒`;
      }else if (totalSeconds >= 60){
        const minutes = Math.floor(totalSeconds / 60);
        const seconds = Math.floor(totalSeconds % 60);
        timeString = `${minutes}分${seconds}秒`;
      }else if (totalSeconds >= 0){
        timeString = `${totalSeconds}秒`;
      }else {
        timeString = '';
      }
      return timeString;
    },
    paddpc(){
      paddpc().then(response => {
         console.log(response);
        if(response.data==0){
          console.log("------------------")
          this.pc=false;
        }else{
           this.pc=true;
        }
      });
    },
    paddsheng(){
      paddsheng().then(response => {
         console.log(response);
        if(response.data==0){
          console.log("------------------")
          this.shang=true;
          this.sheng=false;
        }else{
          this.shang=false;
           this.sheng=true;
        }
      });
    },
    /** 查询任务计划信息列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.paddsheng();
        this.paddpc();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        mesNo: null,
        taskNo: null,
        taskName: null,
        taskNumber: null,
        exStartTime: null,
        exEndTime: null,
        acStartTime: null,
        acEndTime: null,
        exOverTime: null,
        acOverTime: null,
        taskRhythm: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务计划信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务计划信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    //一键生成工单
    oneTouch() {
      this.$modal.confirm('是否确认一键生成任务工单信息？').then(function () {
        return addsheng();
      }).then(() => {
        this.paddsheng();
        this.paddpc();
        this.$modal.msgSuccess("生产成功");
      }).catch(() => { });
    },
      //一键排产
  twoTouch() {
    this.$modal.confirm('是否确认一键排产工单信息？').then(function () {
        return addpc();
      }).then(() => {
        this.paddsheng();
        this.paddpc();
        this.$modal.msgSuccess("排产成功");
      }).catch(() => { });
    },
    /** 删除工单按钮操作 */
    workDelete() {
      this.$modal.confirm('是否确认删除任务工单信息？').then(function () {
        return workdelete();
      }).then(() => {
        this.paddsheng();
        this.paddpc();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务计划信息编号为"' + ids + '"的数据项？').then(function () {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zndd/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "任务计划导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('zndd/task/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style lang="css" scoped>
/* 自定义滚动条样式 */
::v-deep ::-webkit-scrollbar {
  width: 12px; /* 水平滚动条的宽度 */
  height: 12px; /* 垂直滚动条的高度 */
  cursor: pointer; /* 鼠标靠近滚动条时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道的颜色 */
  cursor: pointer; /* 鼠标靠近滚动条轨道时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb {
  background: #888; /* 滚动条滑块的颜色 */
  border-radius: 6px; /* 滚动条滑块的圆角 */
  cursor: pointer; /* 鼠标靠近滚动条滑块时变为小手样式 */
}

::v-deep ::-webkit-scrollbar-thumb:hover {
  background: #555; /* 滚动条滑块悬停时的颜色 */
  cursor: pointer; /* 鼠标悬停在滚动条滑块上时仍保持小手样式 */
}
</style>
